package com.baicaoyuan.web.controller;

import com.baicaoyuan.web.common.CheckPower;
import com.baicaoyuan.web.common.LogUtil;
import com.baicaoyuan.web.common.PlatformIdConfig;
import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.request.br.BJzzUserPageRequest;
import com.baicaoyuan.web.request.br.BSnoRequest;
import com.baicaoyuan.web.request.br.BUserIdRequest;
import com.baicaoyuan.web.response.JzzUserInfoResponse;
import com.baicaoyuan.web.response.JzzUserProfitFlowResponse;
import com.baicaoyuan.web.response.JzzUserResponse;
import com.baicaoyuan.web.service.JzzFlowService;
import com.baicaoyuan.web.service.JzzService;
import com.baicaoyuan.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Api(value = "jzz Controller", tags = {"jzz分润操作接口"})
@RestController
@RequestMapping(value = "/jzz/flow")
public class JzzFlowController {

    @Autowired
    JzzService jzzService;

    @Autowired
    UserService userService;

    @Autowired
    CheckPower checkPower;

    @Autowired
    PlatformIdConfig platformIdConfig;

    @Autowired
    JzzFlowService jzzFlowService;

    @ApiOperation(value = "金种子个人账户信息（token）", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/user/getUserJzzInfo", method = RequestMethod.POST)
    @MemberAccess
    @ResponseBody
    public Status getUserJzzInfo(@RequestBody BUserIdRequest bRequest) {
        try {
            JzzUserResponse response = jzzService.findJzzUser(bRequest.getUserId());
            Status result = new Status(StateMessage.SUCCESS);
            response.setPrice(BigDecimal.valueOf(platformIdConfig.getPrice(), 2));
            result.setData("userInfo", response);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED, ex.getMessage());
        }
    }

    @ApiOperation(value = "后台-金种子用户表-分页(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/user/list", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getJzzUserList(@RequestBody BJzzUserPageRequest request) {
        try {
            List<JzzUserInfoResponse> jzzUsers =
                    jzzService.getJzzUsers(request.getQueryUserId(),request.getPhone(),
                            request.getPage(), request.getSize());
            Integer count = jzzService.getCountJzzUser(request.getQueryUserId());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("jzzUser", jzzUsers);
            result.setData("totalCount", count);
            List l = jzzService.getSumJzz();
            result.setData("JzzSum", l.get(0));
            result.setData("Jzz30Price", jzzService.getJzz30Price().get(0));

            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }

    @ApiOperation(value = "后台-金种子用户表-金种子明细(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/user/flows", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status getUserFlows(@RequestBody BUserIdRequest request) {
        try {
            Status result = new Status(StateMessage.SUCCESS);
            List<JzzUserProfitFlowResponse> re =jzzService.findFlowsByUserId(request.getId());
            result.setData("flows", re);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED);
        }
    }



    @ApiOperation(value = "后台-修改金种子码(token)", notes = "", code = 200, produces = "application/json")
    @RequestMapping(value = "/modify/sno", method = RequestMethod.POST)
    @ResponseBody
    @MemberAccess
    public Status modifySno(@RequestBody BSnoRequest request) {
        try {
            checkPower.checkPower(request.getToken(), request.getUserId());
            Boolean r = jzzFlowService.modifySon(request.getOldSno(), request.getNewSno());
            Status result = new Status(StateMessage.SUCCESS);
            result.setData("result", r);
            return result;
        } catch (Exception ex) {
            LogUtil.Error("", ex);
            return new Status(StateMessage.FAILED, ex.getMessage());
        }

    }


}
